<template>
  <div class="address-manage">
    <search-table :url="url" ref="searchtable" :columns="columns">

      <!-- <template slot="searchForm">
        <a-form layout="inline" :form="form">
          <a-form-item label="角色名称">
            <a-input
              v-decorator="[
                'name',
              ]"
              placeholder="请输入角色名称"
            >
            </a-input>
          </a-form-item>
        </a-form>
      </template> -->
      <template slot="oprationRow">
        <permission authority="address:config:add">
          <a-button type="primary" icon="plus" @click="openInfo()">添加</a-button>
        </permission>
        <permission authority="address:config:delete">
          <a-button @click="deleteRows($refs.searchtable.selectedRowKeys)" type="danger" icon="delete">删除</a-button>
        </permission>
      </template>
      <template slot="tableOperation" slot-scope="{record}">
        <permission authority="address:config:modify">
          <span class="text-primary pointer" @click="openInfo(record)">修改</span>
        </permission>
        <permission authority="address:config:delete">
          <a-divider type="vertical" />
          <span class="text-danger pointer" @click="deleteRows([record.id])">删除</span>
        </permission>
      </template>
    </search-table>
    <address-info  :record="currentRecord" :addressTypeOptions="addressTypeOptions" @cancel="closeInfoModal" :visible="infoVisible" />
  </div>
</template>
<script>
import AddressInfo from './components/AddressInfo'
import { findNameInOptions } from '@/utils'
import { findDictNameByValue,createOptionsByDict } from '@/utils/dict'
export default {
  name: 'AddressManage',
  data () {
    return {
      url: '/address/config/page',
      infoVisible: false,
      currentRecord: {},
      // form: this.$form.createForm(this, { name: 'table_search' }),
      addressTypeOptions: createOptionsByDict('ADDRESS_TYPE'),
      columns: [
        {
          title: '编号',
          dataIndex: 'id'
        },
        {
          title: '地址名称',
          dataIndex: 'addressName'
        },
        {
          title: '地址类型',
          dataIndex: 'addressType',
          customRender: text => findDictNameByValue('ADDRESS_TYPE', text)
        },
        {
          title: '默认地址信息',
          dataIndex: 'isDefault',
          customRender: (text, record) => < a-switch checked = {text == 1} onChange = { () => this.setDefault(record, text)} />
        },
        {
          title: '联系人姓名',
          dataIndex: 'contactName'
        },
        {
          title: '地址',
          dataIndex: 'fullAddress'
        },
        {
          title: '联系电话',
          dataIndex: 'phone'
        },
        {
          title: '操作',
          dataIndex: 'operation',
          scopedSlots: { customRender: 'operation' }
        }]
    }
  },
  components: {
    AddressInfo
  },
  methods: {
    setDefault (record, isDefault) {
      isDefault = isDefault == 1 ? 0 : 1
      const message = {
        0: '请确认是否取消该默认地址？',
        1: '请确认是否设置为该类型下的默认地址？'
      }
      this.$confirm({
        title: '操作提示',
        content: message[isDefault],
        okText: '确认',
        cancelText: '取消',
        onOk: () => {
          this.$post('/address/config/defaultaddress', { id: record.id, addressType: record.addressType, isDefault }).then(res => {
            if (res && res.returnCode == 1000) {
              this.$message.success('操作成功！')
              this.$refs.searchtable.refreshTable()
            }
          })
        }
      })
    },
    closeInfoModal (freshTable) {
      this.infoVisible = false
      if (freshTable) {
        this.$refs.searchtable.refreshTable()
      }
    },
    openInfo (record) {
      this.currentRecord = record
      this.infoVisible = true
    },
    deleteRows (ids) {
      if (!(ids && ids.length)) return this.$message.warn('请先选择一项后再进行操作')
      this.$confirm({
        title: '删除提示',
        content: '删除操作不可恢复，请确认是否删除？',
        okText: '确认删除',
        cancelText: '取消',
        onOk: () => {
          this.$post('/address/config/delete', { ids }).then(res => {
            if (res && res.returnCode == 1000) {
              this.$message.success('删除成功！')
              this.$refs.searchtable.refreshTable()
            }
          })
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>

</style>
